﻿@model SimpleTaskManager.Domain.Entities.Task

@{
    ViewBag.Title = "People";
}

@{ Html.RenderAction("Header", new { id = Model.TaskID, page = 1}); }
<fieldset>
    <legend>People on this task</legend>
    <h4>Leader: @(Model.Leader.FirstName + " " + Model.Leader.LastName)</h4>
    <div id="GroupListBlock"></div>
@if(ViewBag.IsLeader){
        <div class="list_button"><a href="#" class="AssignLink ButtonLink" taskid=@Model.TaskID isSubTask="false">Assign people to this task</a></div>
    }
</fieldset>
<fieldset>
    <legend>Sub Tasks</legend>
    <div id="SubtaskPeopleList"></div>
</fieldset>
@{ Html.RenderAction("Footer"); }

<div id="PeopleDialog" title="" class="Hidden"></div>
<div id="PeopleListDialog" title="" class="Hidden"></div>
<script type="text/javascript">
    $(function () {
        $("#PeopleDialog").dialog({
            autoOpen: false, width: 400, height: 330, modal: true,
            buttons: {
                "Save": {
                    id: "save-button",
                    text: "Save",
                    click: function () {
                        //hide the save button and show the loader
                        $('#save-button').hide();
                        $('#ajaxBusy').show();

                        if ($(this).data("data").isSub == "false") {
                            $.post("/Task/AssignPeople/" + $(this).data("data").id,
                            $("#PeopleForm").serialize(),
                            function () {
                                //show the save button and hide the loader
                                $('#save-button').show();
                                $('#ajaxBusy').hide();

                                $("#PeopleDialog").dialog("close");
                                LoadGroupList();
                            });
                        }
                        else {
                            $.post("/Task/AssignPeople/" + $(this).data("data").id,
                            $("#PeopleForm").serialize(),
                            function () {
                                //show the save button and hide the loader
                                $('#save-button').show();
                                $('#ajaxBusy').hide();

                                $("#PeopleDialog").dialog("close");
                                LoadList();
                            });
                        }
                    }
                },
                Cancel: function () { $(this).dialog("close"); }
            },
            create: function () {
                $('div.ui-dialog-buttonset').prepend('<div id="ajaxBusy" style="display:inline-block; margin: .5em 1.4em .5em 0; width:16px; height: 16px"><p><img src="../../Content/images/ajax-loader.gif"></p></div>');
                $('#ajaxBusy').hide();
            }
        });
        $("#PeopleListDialog").dialog({
            autoOpen: false, width: 400, height: 330, modal: true,
            buttons: {
                "Close" : function () { $(this).dialog("close"); }
            }
        });
        $(".AssignLink").live("click", function () {
            var id = $(this).attr("taskid");
            var isSubTask = $(this).attr("isSubTask");
            var data = { id: id, isSub: isSubTask };
            $("#PeopleDialog").html("")
                .dialog("option", "title", "Manage assigned people")
                .load("/Task/AssignPeople/" + id, function ()
                    { $("#PeopleDialog").data("data", data).dialog("open"); });
        });
        $(".peoplelist").live("click", function () {
            var t_id = $(this).attr("taskid");
            $("#PeopleListDialog").html("")
                .dialog("option", "title", "Group")
                .load("/Task/PeopleListPartial/" + t_id, function () { $("#PeopleListDialog").dialog("open"); });
        });
        LoadGroupList();
        LoadList();
    });
    function LoadList() {
        $("#SubtaskPeopleList").load("/Task/SubtaskPeopleList/@(Model.TaskID)", function ()
        { $("#taskTable").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });/*.tablesorter({ sortList: [[0, 0]] });*/ });
    }
    function LoadGroupList() {
        $("#GroupListBlock").load("/Task/GroupList/@(Model.TaskID)");
    }
</script>
